<template>
  <div>
    <!-- 定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>教师</el-breadcrumb-item>
      <el-breadcrumb-item>教师资源管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 定义卡片 -->
    <el-card class="box-card">
      <!-- 定义第一行数据 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryItemInfo.query" clearable @clear="getItemList">
            <el-button slot="append" icon="el-icon-search" @click="getItemList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="toAddItem = true">添加教师</el-button>
        </el-col>
      </el-row>

      <!-- 获取商品列表数据 -->
      <el-table :data="itemList" style="width: 100%" stripe border>
        <el-table-column type="index" label="序号" width="50px">
        </el-table-column>
        <el-table-column prop="title" label="姓名" width="100px">
        </el-table-column>
        <el-table-column prop="sellPoint" label="职位" width="100px">
        </el-table-column>
        <el-table-column prop="info" label="个人简介" width="400px">
        </el-table-column>
        <el-table-column prop="sign" label="个人标签" width="100px">
        </el-table-column>
        <!-- 使用过滤器修改时间 在main.js中定义过滤器 -->
        <el-table-column prop="price" label="评价等级" width="100px">
          <template slot-scope="scope">
            {{scope.row.price | priceFormat}}
          </template>
        </el-table-column>
        <el-table-column prop="num1" label="专业度" width="80px">
        </el-table-column>
        <el-table-column prop="num2" label="回复率" width="80px">
        </el-table-column>
        <el-table-column prop="num3" label="服务度" width="80px">
        </el-table-column>

       <!-- <el-table-column prop="updated" label="更新时间" width="180px" :formatter="formatDate">
        </el-table-column> -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="updateItemBtn(scope.row)">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteItemBtn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 定义分页插件-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryItemInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryItemInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

      <el-dialog title="修改教师信息" :visible.sync="updateItemDialogVisible" width="50%">
        <!-- 定义分类表单 -->
         <el-form :model="updateItemForm" :rules="rules" label-width="100px">
          <el-form-item label="姓名:" prop="title">
            <el-input v-model="updateItemForm.title"></el-input>
          </el-form-item>
          <el-form-item label="职位:" prop="sellPoint">
            <el-input v-model="updateItemForm.sellPoint"></el-input>
          </el-form-item>
          <el-form-item label="评价等级:" prop="price">
            <el-input v-model="updateItemForm.price"></el-input>
          </el-form-item>
          <el-form-item label="个人简介:" prop="info">
            <el-input v-model="updateItemForm.info"></el-input>
          </el-form-item>
          <el-form-item label="标签:" prop="sign">
            <el-input v-model="updateItemForm.sign"></el-input>
          </el-form-item>
          <el-form-item label="专业度:" prop="num1">
            <el-input v-model="updateItemForm.num1"></el-input>
          </el-form-item>
          <el-form-item label="回复率:" prop="num2">
            <el-input v-model="updateItemForm.num2"></el-input>
          </el-form-item>
          <el-form-item label="服务度:" prop="num3">
            <el-input v-model="updateItemForm.num3"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="updateItemDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateItemCat">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 新增-->
      <el-dialog title="新增教师资源" :visible.sync="toAddItem" width="50%" @close="closeAddItemCatDialog">
        <!-- 定义分类表单 -->
        <el-form :model="itemCatForm" :rules="rules" ref="itemCatFormRef" label-width="100px">
          <el-form-item label="分类名称:" prop="name">
            <el-input v-model="itemCatForm.name"></el-input>
          </el-form-item>
          <!-- 定义父级分类选项 -->
          <el-form-item label="父级分类名称:">
            <!-- 通过级联选择器定义1/2级商品分类
               步骤:  1.注意标签导入
                      2.options 表示数据的来源
                      3.指定props属性指定数据配置
            -->
            <el-cascader v-model="selectedKeys" :props="props" :options="parentItemCatList" clearable
              @change="parentItemCatChange"></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addItemCatDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addItemCatForm">确 定</el-button>
        </span>
      </el-dialog>
  </div>

</template>

<script>
  export default {
    data() {
      return {
        //1.定义商品列表信息
        itemList: [],
        toAddItem: false,
        //2.定义分页对象
        queryItemInfo: {
          query: '', //定义查询参数
          pageNum: 1,
          pageSize: 10
        },
        total: 0,
        updateItemDialogVisible:false,
        updateItemForm:{},
        //校验
        rules: {
          name: [{
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }]
        },
      }
    },
    created() {
      //1.获取商品列表数据
      this.getItemList()
    },
    methods: {
      //实现商品信息分页查询
      async getItemList() {
        const {
          data: result
        } = await this.$http.get("/item/getItemList", {
          params: this.queryItemInfo
        })
        if (result.status !== 200) return this.$message.error("商品列表查询失败")
        this.itemList = result.data.rows
        this.total = result.data.total
      },
      //通过JS格式化时间
      formatDate(row, column, cellValue, index) {
        let date = new Date(cellValue)
        let year = date.getFullYear()
        let month = (date.getMonth() + 1 + '').padStart(2, '0')
        let day = (date.getDate() + '').padStart(2, '0')
        let HH = (date.getHours() + '').padStart(2, '0')
        let MM = (date.getMinutes() + '').padStart(2, '0')
        let SS = (date.getSeconds() + '').padStart(2, '0')
        return year + '-' + month + '-' + day + ' ' + HH + ":" + MM + ":" + SS
      },
      //条数变化时 调用
      handleSizeChange(size) {
        this.queryItemInfo.pageSize = size
        this.getItemList()
      },
      //页码值变化时 调用
      handleCurrentChange(current) {
        this.queryItemInfo.pageNum = current
        this.getItemList()
      },
      async updateStatus(item) {
        const {
          data: result
        } = await this.$http.put("/item/updateItemStatus", {
          id: item.id,
          status: item.status
        })
        if (result.status !== 200) return this.$message.error("更新状态失败")
        this.$message.success("更新状态成功")
      },
      async deleteItemBtn(item) {
        //消息确认框
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          //根据id删除数据
          const {
            data: result
          } = await this.$http.delete("/item/deleteItemById", {
            params: {
              id: item.id
            }
          })
          if (result.status !== 200) return this.$message.error("教师删除失败")
          this.$message.success("教师删除成功")
          //重新获取教师列表信息
          this.getItemList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      //转向到教师新增页面
      // toAddItem(){
        // this.$router.push("/item/addItem")
      // },
      updateItemBtn(item){
        /* 扩展练习*/
         this.updateItemForm = item
         this.updateItemDialogVisible = true
        console.log("扩展案例,自己实现 只需要修改 标题/卖点/价格/数量")
      },
      async updateItemCat() {
              //修改教师信息
              const {
                data: result
              } = await this.$http.put('/item/updateItem', this.updateItemForm)
              if (result.status !== 200){ return this.$message.error("更新教师信息失败")}
              this.$message.success("更新教师信息成功")
              this.updateItemDialogVisible = false;
              this.findItemCatList();
            },
    }
  }
</script>
<!-- 防止组件样式冲突 -->
<style lang="less" scoped>
  .el-card {
  	margin-top: 15px;
  	box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
  }

  /* 定义表格上边距*/
  .el-table {
  	margin-top: 15px;
  }

  /* 定义分页上边距*/
  .el-pagination {
  	margin-top: 15px;
  	text-align: center;
  }

  /* 定义富文本编辑器的宽度 */
  .ql-editor {
  	min-height: 350px;
  }

  video {
  	min-height: 200px;
  	min-width:  100%;
  }
  .el-table {
  	margin-top: 15px;
  }

  /* 定义分页上边距*/
  .el-pagination {
  	margin-top: 15px;
  	text-align: center;
  }

  /* 定义富文本编辑器的宽度 */
  .ql-editor {
  	min-height: 350px;
  }

  video {
  	min-height: 200px;
  	min-width:  100%;
  }
</style>
